package com.ktjy.controller;

import com.ktjy.service.SysUserService;
import com.pojo.SysUser;
import jdk.vm.ci.meta.Constant;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
@Scope("prototype")
public class SysUserController {
    private Logger logger = Logger.getLogger(SysUserController.class.getName());
    private ArrayList<SysUser> userlist = new ArrayList<>();
    //用于返回查询到用户数据
    private ArrayList<SysUser> queryUserList = new ArrayList<SysUser>();
    @Resource
    private SysUserService sysUserService;
    @Autowired
    private HttpSession Session;

    //跳转到登录页面
    @RequestMapping("/tpLogin")
    public String login() {
        return "login";
    }

    //登录
    @PostMapping("/login")
    public String doLogin(@RequestParam(value = "account")String account, @RequestParam(value = "password")String password) {
        //调用service方法，进行用户匹配
        SysUser sysUser = sysUserService.login(account, password);
        if (null != sysUser) {
            //登入成功，重定向到/user/toMain接口
            return "redirect:/user/index";
        }else {
            return "login";
        }
    }
    @RequestMapping("/toMain")
    public String main(){
        return "frame";
    }
    public SysUserController() {
        try {
            userlist.add(new SysUser(5,"cts","夏侯涛","5555555",1,new Date(),"上海市宝山区",1,1,new Date(),1,new Date()));
            userlist.add(new SysUser(6,"cts","随风大主播","66666",2,new Date(),"上海市陆家嘴",2,2,new Date(),2,new Date()));

        } catch (Exception e) {
           e.printStackTrace();
        }
    }
    //查询所有的用户列表
    @GetMapping("/list")
    public String list(Model model) {
        logger.info("当前查询条件为空时，查询用户信息"+this);
        queryUserList.clear();
        queryUserList.addAll(userlist);
        model.addAttribute("queryUserList",userlist);
        return "sysUser/sysUserList";
    }
    //根据用户名称查询用户列表
    @PostMapping("/list")
    public String list(@RequestParam(value = "realName",required = false) String realName, Model model) {
        logger.info("查询条件:[realName]="+realName+",查询用户信息");
        queryUserList.clear();
        if(realName!=null&&!realName.equals("")){
            for(SysUser user:userlist){
                if(user.getRealName().indexOf(realName)!=-1){
                    queryUserList.add(user);
                }
            }
        }else {
            queryUserList.addAll(userlist);
        }
        model.addAttribute("queryUserList",queryUserList);
        return "sysUser/sysUserList";
    }
    public String doLogin(@RequestParam(value = "account")String account, @RequestParam(value ="password")String password, HttpServletRequest request) {
        SysUser sysUser = sysUserService.login(account, password);
        if (null != sysUser) {
           Session.setAttribute("user", sysUser);
           return "redirect:/user/index";
        }else {
            return "login";
        }
    }
    public String main(HttpSession session){
        if(session.getAttribute("user")==null){
            return "redirect:/user/toLogin";
        }
        return "frame";
    }
    @ExceptionHandler(value = {RuntimeException.class})
    public String handlerException(RuntimeException e,HttpServletRequest req){
        req.setAttribute("e",e);
        return "error";
    }
    public String exLogin(@RequestParam String account,@RequestParam String password){
        SysUser sysUser = sysUserService.login(account, password);
        if (null != sysUser) {
            throw new RuntimeException("用户名或密码不正确，跳转到错误页面！");
        }
        return "redirect:/user/toMain";
    }

//    public String getUserList(Model model,String queryRealName,Integer queryRoleId,@RequestParam(defaultValue = "1")Integer pageIndex) {
//        List<SysUser> userList=null;
//        try {
//            int pageSize= Constants.pageSize;
//            int totalCount=sysUserService.getUserCount(queryRealName,queryRoleId);
//            PageSupport pages=new PageSupport();
//            pages.setPageSize(pageSize);
//            pages.setTotalCount=pages.getTotalCount();
//            if(pageIndex>totalCount) {
//                pageIndex = totalCount;
//                throw new RuntimeException("页码不正确");
//            }
//            userList
//= sysUserService.getUserList(queryRealName, queryRoleId, pageIndex, pageSize);
//            model.addAttribute("userList", userList)
//              List<SysRole> roleList=null;
//              roleList=sysRoleService.getRoleList();
//            model.addAttribute("roleList", roleList);
//            model.addAttribute("queryRealName", queryRealName);
//            model.addAttribute("queryRoleId", queryRoleId);
//            model.addAttribute("pages", pages);

    //        } catch (Exception e) {
//                  e.printStackTrace();
    //            logger.error("获取用户列表失败", e);
//            return "redirect:/user/toSysError";";
//        }
//      return "sysUser/list";
//    }
    }
